package lc.二叉树;

import utils.Node;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Math589_N叉树的前序遍历 {

    public List<Integer> preorder(Node root) {

            List<Integer> res = new ArrayList<>();
            Stack<Node> stack = new Stack<>();
            if(root == null)
            return res;

            stack.push(root);
            while (!stack.isEmpty()) {
                Node node = stack.pop();
                res.add(node.val);
                for (int size = node.children.size() - 1; size >= 0; size--) {
                    stack.add(node.children.get(size));
                }
            }
            return res;

    }


    List<Integer> res = new ArrayList<Integer>();
    public List<Integer> preorder1(Node root) {

        if (root == null) {
            return res;
        }
        res.add(root.val);
        for(Node node : root.children) {

            preorder1(node);
        }
        return res;
    }
}
